package com.abl.core.common.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
@Configuration
public class ContextKit implements ApplicationContextAware, EnvironmentAware {
    private static ApplicationContext applicationContext = null;

    public static ApplicationContext getContext() throws BeansException {
        return ContextKit.applicationContext;
    }

    /**
     * 1.通过实例变量名获取实例
     *
     * @param beanName 实例变量名
     */
    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    /**
     * 2.通过class获取单个Bean实例
     *
     * @param tClass 类型
     */
    public static <T> T getBean(Class<T> tClass) {
        return applicationContext.getBean(tClass);
    }

    /**
     * 3.通过name,以及Clazz返回指定的Bean
     *
     * @param beanName 实例变量名
     * @param tClass   类型
     */
    public static <T> T getBean(String beanName, Class<T> tClass) {
        return applicationContext.getBean(beanName, tClass);
    }

    /**
     * 4.是否存在实例
     *
     * @param beanName 实例变量名
     */
    public static boolean contains(String beanName) {
        return applicationContext.containsBean(beanName);
    }

    /**
     * 5.是否为单例
     *
     * @param beanName 实例变量名
     */
    public static boolean isSingle(String beanName) {
        return applicationContext.isSingleton(beanName);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ContextKit.applicationContext = applicationContext;
    }


    private static Environment env = null;

    /**
     * 读取配置文件中的属性值
     *
     * @param propertyKey 属性Key
     */
    public static String getProperty(String propertyKey) {
        return ContextKit.env.getProperty(propertyKey);
    }

    /**
     * 上下文路径
     * 默认 ""
     */
    public static String contextPath() {
        String property = ContextKit.env.getProperty("server.servlet.context-path");
        return property == null ? "" : property;
    }

    @Override
    public void setEnvironment(Environment environment) {
        ContextKit.env = environment;
    }


}
